<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;


class Admin extends Base {
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $auto = ['status','code','create_time', 'last_login_ip'];
    protected static $_field = [ 'id','username','nickname','hash','password','gid','status','create_time','update_time'];

    public function setCreateTimeAttr(){
        return time();
    }

    public static function getMember($data){
        return is_array($data) ? self::field(self::$_field)->where($data)->find(): self::field(self::$_field)->find($data);
    }

    public static function all($data = null, $with = [], $cache = false){
        return self::field(self::$_field)->where($data)->with($with)->cache($cache)->select();
    }

    protected function setPasswordAttr($value){
        return substr($value,10,15);
    }

    public function setLastLoginIpAttr(){
        return request()->ip();
    }

    protected function getLastDateAttr($value){
        return $value?date('Y-m-d H:i:s',$value):0;
    }

    public function getMidAttr($value){
        $integerIDs = array_map('intval', explode(',', $value));
        return $integerIDs;
    }

    public function getCodeAttr($value){
        $code = explode(',',$value);
        return $code;
    }

    public function setNicknameAttr($value){
        if(empty($value)) return '';
        if(is_special_char($value)){
            return emoji_encode($value);
        }else{
            return $value;
        }
    }

    public function getNicknameAttr($value){
        if(empty($value)) return '';
        if(is_special_char($value)){
            return emoji_decode($value);
        }else{
            return $value;
        }
    }

}